package main

import (
	"fmt"
	"go/parser"
	"go/token"
	"os"
)

func main() {
	fmt.Println("-------------------------")
	constantName := "Greeting"
	constantValue := "Hello, Gophers!"

	// 生成 Go 代码，用于替换常量的值
	output := fmt.Sprintf("package mypackage\n\nconst (\n    %s = \"%s\"\n)\n", constantName, constantValue)

	// 将生成的代码写入 constants.go 文件
	constantsFile, err := os.Create("constants.go")
	if err != nil {
		fmt.Println("Error creating constants.go:", err)
		os.Exit(1)
	}
	defer constantsFile.Close()

	_, err = constantsFile.WriteString(output)
	if err != nil {
		fmt.Println("Error writing to constants.go:", err)
		os.Exit(1)
	}

	fset := token.NewFileSet()
	node, err := parser.ParseFile(fset, "your_file.go", nil, parser.ParseComments)

	fmt.Println(node)
}
